<%@page import="com.util.HTClient,java.io.IOException"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.net.URLEncoder"%>
<%@page import="listener.PayLinstener"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="com.util.GetIP,hyl.core.MyFun,java.util.Map" %>
<%@page import="java.io.PrintWriter" %>
<%@page import="com.alibaba.fastjson.*" %>
<%@page import="com.wxpay.config.*" %>
<%@page import="com.wxpay.config.WxPayApi.*" %>
<%@page import="com.wxpay.util.*" %>
<%
	/* 这是微信内部浏览器调用 */
	response.setHeader("Access-Control-Allow-Origin","*");
	response.setContentType("text/json; charset=utf-8");
	response.setHeader("Access-Control-Allow-Methods","GET,POST,OPTIONS,DELETE");
	response.setHeader("Access-Control-Max-Age","3600");
	response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
	String callback=request.getParameter("callback");
	if(null==callback){
	  System.out.println("不是跨域请求");
	}else{
	  System.out.println("是跨域请求");
	}

	PrintWriter shuchu = response.getWriter();
	JSONObject maps = PayLinstener.map;
	try {
		String code = request.getParameter("code");
		String wxcode = request.getParameter("wxcode");
		JSONObject map = maps.getJSONObject(code);
		if(map==null){
			return;
		}
		JSONObject wx = map.getJSONObject("wx");
		
		Map mmm = new HashMap();
		mmm.put("url", "https://api.weixin.qq.com/sns/oauth2/access_token");
		mmm.put("appid",wx.getString("appID"));
		mmm.put("secret",wx.getString("appsecret"));
		mmm.put("code",wxcode);
		mmm.put("grant_type","authorization_code");
		
		String retu = HTClient.Send(mmm);
		JSONObject job = JSONObject.parseObject(retu);
		System.out.println(JSON.toJSONString(job));
		String WIDtotal_amount = request.getParameter("WIDtotal_amount");
		String appID = wx.getString("appID");
		String mchID = wx.getString("mchID");
		String subappID = wx.getString("sub_appid");
		String submchID = wx.getString("sub_mch_id");
		String partnerKey = wx.getString("partnerKey");
		String notifyUrl = wx.getString("notifyUrl");
		System.out.println(notifyUrl);
		// 获取会员ip地址(有的手机可以直接获取到真实ip,大部分都是有代理的)
		String ip = GetIP.getIpAddress(request);
		
		/**
		 * 重头戏：拼接微信微信支付参数 有的是数据库获取的,这里我是直接写死了,比如商品详情,付款金额之类的
		 */
		WxPayConf params = new WxPayConf();
		// AppId
		params.setAppId(appID);
		// 商户号
		params.setMchId(mchID);
		//setSubOpenId
		params.setOpenId(job.getString("openid"));
		//setSubAppId 子商户appid
		params.setSubAppId(subappID);
		//setSubMchId 子商户号 
		params.setSubMchId(submchID);
		// 商品简单描述(订单名)
		params.setBody(map.getString("name"));
		// 用户端IP
		params.setSpbillCreateIp(ip);
		// 订单总金额，单位为分
		params.setTotalFee(WIDtotal_amount);
		// H5支付的交易类型为MWEB(默认)
		params.setTradeType(TradeType.JSAPI);
		// 微信支付成功后的回调Url
		params.setNotifyUrl(notifyUrl);
		// 商户秘钥
		params.setPaternerKey(partnerKey);
		// 随机字符串，不长于32位(我这里直接获取当前时间毫秒值)
		String TradeNo = request.getParameter("WIDout_trade_no");
		params.setOutTradeNo(TradeNo);
		Map<String, String> build = params.BuildPt();
		/**
		 * 直接用微信统一下单工具类,并接收返回的Xml,返回内容详见微信支付开发文档
		 */
		String xmlResult = WxPayApi.pushOrder(false, build);
		Map<String, String> result = WXPayUtil.xmlToMap(xmlResult);
		// 返回状态码
		String return_code = result.get("return_code");
		// 返回状态码
		String return_msg = result.get("return_msg");
		//System.out.println(JSON.toJSONString(result));
		
		if (MyFun.isEmpty(return_code)||!return_code.contains("SUCCESS")){
			//throw new RuntimeException(return_msg);
			shuchu.print("error");
			return;
		}
		String prepayId = result.get("prepay_id");
		
		Map<String, String> payMap = new HashMap<String, String>();
		payMap.put("appId", appID);  
		payMap.put("timeStamp", WXPayUtil.getCurrentTimestamp()+"");  
		payMap.put("nonceStr", WXPayUtil.generateNonceStr());  
		payMap.put("signType", "MD5");  
		payMap.put("package", "prepay_id=" + prepayId);  
		String paySign = WXPayUtil.generateSignature(payMap, partnerKey);  
		payMap.put("paySign", paySign);
		shuchu.println(callback+"("+JSON.toJSONString(payMap)+")");
		shuchu.flush();
		shuchu.close();
	}catch (IOException e) {
		e.printStackTrace();
	}
%>